package com.efast.dmom.aps.datahandlers;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.efast.cafe.framework.util.SpringWebUtils;
import com.efast.dmom.aps.bean.WorkCenter;
import com.efast.dmom.aps.dao.WorkCenterDao;
import com.efast.dmom.aps.entity.WorkCenterEntity;
import com.efast.dmom.aps.util.TimeUtil;
@Component
public class WorkCenterHandler {
	
	@Autowired
	private WorkCenterDao workCenterDao;
	public void save(WorkCenter obj) {
		if (obj.getInnerData() != null) {
			obj.getInnerData().setSiteId(DataHandlerUtils.getSiteId());
			obj.getInnerData().setCreationTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setCreationTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setCreator(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
//			obj.getInnerData().setCreationPid(DataHandlerUtils.getDmomPid());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());
			obj.getInnerData().setRowStatus(1);
			obj.getInnerData().setRowTrxId(111);
			workCenterDao.save(obj.getInnerData());
		}
	}

	public void update(WorkCenter obj) {

		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			workCenterDao.update(obj.getInnerData());
		}
	}

	public void delete(WorkCenter obj) {
		if (obj.getInnerData() != null) {

			obj.getInnerData().setLastModifiedTime(TimeUtil.getLocalDateTime());
			obj.getInnerData().setLastModifier(SpringWebUtils.getRequestUser());
			obj.getInnerData().setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
//			obj.getInnerData().setLastModifiedPid(DataHandlerUtils.getDmomPid());

			workCenterDao.delete(obj.getInnerData());
		}
	}

	public WorkCenter getById(long id) {
		WorkCenter workCenter = new WorkCenter();
		workCenter.setInnerData(workCenterDao.getById(id));
		return workCenter;
	}

	public WorkCenter getByName(String name) {
		WorkCenter workCenter = new WorkCenter();
		workCenter.setInnerData(workCenterDao.getByName(name));
		return workCenter;
	}


	public WorkCenter getByCode(String workshopCode) {
		WorkCenter workCenter = new WorkCenter();
		workCenter.setInnerData(workCenterDao.getByCode(workshopCode));
		return workCenter;
	}
	
	public int queryEquipmentById (Long id) {
		int queryProductionLineById = workCenterDao.queryEquipmentById(id);
		return queryProductionLineById;
	}

	public int queryStationById (Long id) {
		int queryProductionLineById = workCenterDao.queryStationById(id);
		return queryProductionLineById;
	}

	public List<WorkCenter> getAllWorkCenter() {
		List<WorkCenterEntity> entityList = workCenterDao.getAllWorkCenter();
		List<WorkCenter> workCenterList = new ArrayList<WorkCenter>();
		if (entityList != null && entityList.size() > 0) {
		for (WorkCenterEntity entityData:entityList) {
			WorkCenter workCenter=new WorkCenter();
			workCenter.setInnerData(entityData);
			workCenterList.add(workCenter);
		}
		return workCenterList;
		}
		return null;
	}
}
